package com.lagou.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 13771
 * @date 2020/9/1 0:34
 * @description
 */
@Component
public class RpcServerApplicationContextAware implements ApplicationContextAware {
    public static Map<String,Object> serviceMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Service.class);
        for (Object serviceBean : beans.values()){
            Class<?> classz = serviceBean.getClass();
            //获取接口
            Class<?>[] interfaces = classz.getInterfaces();
            for (Class<?> inter : interfaces){
                String interfaceName = inter.getName();
                serviceMap.put(interfaceName,serviceBean);
            }
        }
    }
}
